package com.kun.video.ui.window;

import com.kun.video.context.ContextService;
import com.kun.video.domain.SettingSaveDTO;
import com.kun.video.domain.StageSize;
import com.kun.video.thread.ComThreadPool;
import com.kun.video.ui.ComponentUtil;
import com.kun.video.ui.KunComponent;
import com.kun.video.ui.SampleView;
import com.kun.video.ui.control.button.KunButton;
import com.kun.video.ui.pane.KunHorBox;
import com.kun.video.ui.pane.TitleVerBox;
import com.kun.video.util.ChooserUtil;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * 设置窗口
 * 示例子类结构 -> {@link SampleView}
 *
 * @author gzc
 * @since 2025/3/20
 **/
public class SettingView extends VBox implements KunComponent {
    private static final Logger log = LoggerFactory.getLogger(SettingView.class);
    private static final String CSS_CLASS = "setting-view";

    private final TitleVerBox globalVerBox = new TitleVerBox("全局设置");


    private final TitleVerBox outputVerBox = new TitleVerBox("输出设置");
    private final KunButton outputDirButton = new KunButton("输出目录", "选择输出目录");
    private final Label outputDirLabel = new Label();

    private ComThreadPool comThreadPool;
    private SettingSaveDTO settingSaveDTO;

    public SettingView() {
        initialize();
    }

    public SettingView(StageSize size) {
        this();
        setMinSize(size.getMinWidth(), size.getMinHeight());
        setPrefSize(size.getPrefWidth(), size.getPrefHeight());
        setMaxSize(size.getMaxWidth(), size.getMaxHeight());
    }

    @Override
    public Parent getNode() {
        return this;
    }

    @Override
    public String getStyleName() {
        return CSS_CLASS;
    }

    @Override
    public void initStyleClass() {
        KunComponent.super.initStyleClass();
    }

    @Override
    public void initLayout() {
        KunHorBox kunHorBox = new KunHorBox(outputDirButton, outputDirLabel);
        outputVerBox.getChildren().addAll(kunHorBox);
        getChildren().addAll(globalVerBox, outputVerBox);
    }

    @Override
    public void initStyle() {
    }

    @Override
    public void initDimensions() {
    }

    @Override
    public void reactiveDimensions(double width, double height) {
    }

    /**
     * 选择输出文件的目录
     */
    private final EventHandler<? super MouseEvent> selectOutputDirEventHandler = event -> {
        File file = ChooserUtil.showDir("选择输出文件目录");
        if (file != null) {
            settingSaveDTO.setOutputDir(file.getAbsolutePath());
            outputDirLabel.setText(file.getAbsolutePath());
        }
    };

    @Override
    public void initListeners() {
        ComponentUtil.weakActionEventHandler(outputDirButton, MouseEvent.MOUSE_RELEASED, selectOutputDirEventHandler);

    }

    @Override
    public void dispose() {
        ComponentUtil.removeWeakListener(outputDirButton, MouseEvent.MOUSE_RELEASED, selectOutputDirEventHandler);

    }

    @Override
    public void initData() {
        comThreadPool = ContextService.getBean(ComThreadPool.class);
        settingSaveDTO = new SettingSaveDTO();
        outputDirLabel.setText(settingSaveDTO.getOutputDir());
    }


    public void save() {

    }
}
